﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgileSaude.Entities;
using AgileSaude.Business;
using System.Reflection;

namespace AgileSaude.Web.Clinica
{
    public partial class ConsultaServicos : PageBase
    {
        private string sortExpression
        {
            get { return ViewState["sortExpression"].ToString(); }
            set { ViewState["sortExpression"] = value; }
        }
        private int sortDirection
        {
            get { return Convert.ToInt32(ViewState["sortDirection"]); }
            set { ViewState["sortDirection"] = value; }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            sortExpression = "Data";
            sortDirection = 1;
        }

        protected void btnConsultar_Click(object sender, EventArgs e)
        {
            PreencheGrid();
        }

        private void PreencheGrid()
        {
            try
            {
                DateTime dtIni = DateTime.Parse("01/" + txtDataIni.Text);
                DateTime dtFim = DateTime.Parse(DateTime.DaysInMonth(dtIni.Year, dtIni.Month) + "/" + txtDataIni.Text);

                //DateTime dtIni = DateTime.Parse(txtDataIni.Text);
                //DateTime dtFim = DateTime.Parse(txtDataFim.Text);
                int idMedico = 0;
                if (getUsuarioLogado().idMedico.HasValue)
                    idMedico = getUsuarioLogado().idMedico.Value;

                List<PedidoEntities> Pedidos = new PedidoFacade().Listar(dtIni, dtFim, idMedico);

                ((Label)Master.FindControl("lblMensagem")).Visible = false;
                OrdenarLista(ref Pedidos);
                gvConsulta.DataSource = Pedidos;
                gvConsulta.DataBind();
                gvConsulta.Visible = true;
            }
            catch (Exception ex)
            {
                gvConsulta.Visible = false;
                ((Label)Master.FindControl("lblMensagem")).Text = ex.Message;
                ((Label)Master.FindControl("lblMensagem")).CssClass = "failureNotification";
                ((Label)Master.FindControl("lblMensagem")).Visible = true;
            }
        }

        private void OrdenarLista(ref List<PedidoEntities> listaCidade)
        {
            listaCidade.Sort(delegate(PedidoEntities a1, PedidoEntities a2)
            {
                Type t = typeof(PedidoEntities);
                PropertyInfo prop = t.GetProperty(sortExpression);
                object u1Value = prop.GetValue(a1, null);
                object u2Value = prop.GetValue(a2, null);
                return ((IComparable)u1Value).CompareTo(((IComparable)u2Value)) * sortDirection;
            });
        }

        protected void gvConsulta_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvConsulta.PageIndex = e.NewPageIndex;
            PreencheGrid();
        }

        protected void gvConsulta_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //switch (e.CommandName)
            //{
            //    case "Editar":
            //        this.ReadOnly = false;
            //        this.IdCidade = Convert.ToInt32(gvCidades.DataKeys[int.Parse(e.CommandArgument.ToString())]["IdCidade"].ToString());
            //        Server.Transfer("CidadeEdit.aspx", false);
            //        break;
            //    case "Visualizar":
            //        this.ReadOnly = true;
            //        this.IdCidade = Convert.ToInt32(gvCidades.DataKeys[int.Parse(e.CommandArgument.ToString())]["IdCidade"].ToString());
            //        Server.Transfer("CidadeEdit.aspx", false);
            //        break;
            //    default:
            //        break;
            //}
        }

        protected void gvConsulta_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //try
            //{
            //    if (e.Row.RowType == DataControlRowType.DataRow)
            //    {
            //        LinkButton lnkEditar = e.Row.FindControl("lnkEditar") as LinkButton;
            //        lnkEditar.CommandArgument = e.Row.RowIndex.ToString();
            //        ((ScriptManager)Master.FindControl("ScriptManager1")).RegisterPostBackControl(lnkEditar);

            //        LinkButton lnkVisualizar = e.Row.FindControl("lnkVisualizar") as LinkButton;
            //        lnkVisualizar.CommandArgument = e.Row.RowIndex.ToString();
            //        ((ScriptManager)Master.FindControl("ScriptManager1")).RegisterPostBackControl(lnkVisualizar);

            //ImageButton imgEditar = e.Row.FindControl("imgEditar") as ImageButton;
            //imgEditar.CommandArgument = e.Row.RowIndex.ToString();
            //((ScriptManager)Master.FindControl("ScriptManager1")).RegisterPostBackControl(imgEditar);

            //ImageButton imgVisualizar = e.Row.FindControl("imgVisualizar") as ImageButton;
            //imgVisualizar.CommandArgument = e.Row.RowIndex.ToString();
            //((ScriptManager)Master.FindControl("ScriptManager1")).RegisterPostBackControl(imgVisualizar);
            //    }
            //}
            //catch
            //{
            // Response.Redirect("~/sessaoExpirada.aspx", true);
            //}
        }

        protected void gvConsulta_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sortExpression != e.SortExpression)
                sortDirection = 1; //ASC
            else
                sortDirection *= -1; //DESC

            sortExpression = e.SortExpression;
            PreencheGrid();
        }
    }
}